// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import router from './router'
import App from './App'
import { AjaxPlugin, WechatPlugin, AlertPlugin, LoadingPlugin } from 'vux'
import server from './config/server'

global.server = server

Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(AjaxPlugin)
Vue.use(WechatPlugin)
Vue.use(AlertPlugin)
Vue.use(LoadingPlugin)

Vue.http.get(server.wechatJSConfig, {}).then(response => {
    Vue.wechat.config(response.data.data)

    Vue.wechat.ready(function () {
        // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
        Vue.wechat.hideMenuItems({
            menuList: [
                'menuItem:copyUrl',
                'menuItem:originPage',
                'menuItem:openWithQQBrowser',
                'menuItem:openWithSafari',
                'menuItem:share:email'
            ] // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
        })
    })
})

FastClick.attach(document.body)

Vue.config.productionTip = false

const store = new Vuex.Store({})

store.registerModule('vux', {
    state: {
        isLoading: false
    },
    mutations: {
        updateLoadingStatus (state, payload) {
            state.isLoading = payload.isLoading
        }
    }
})

router.beforeEach(function (to, from, next) {
    store.commit('updateLoadingStatus', {isLoading: true})

    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title
    }

    next()
})

router.afterEach(function (to) {
    store.commit('updateLoadingStatus', {isLoading: false})
})

/* eslint-disable no-new */
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app-box')
